module.exports = options => {
    return async function(ctx, next) {
        let token = ctx.cookies.get('token', {
            signed: true,
        });
        let uid = ctx.cookies.get('uid', {
            signed: true,
        });

        if (token) {
            try {
                let decode = ctx.app.jwt.verify(token, options.secret); //验证token
                console.log(decode)
                if (decode.id == uid) {
                    await next();
                } else {
                    ctx.status = 401;
                    ctx.body = {
                        message: '非法token'
                    }
                }
            } catch (error) {
                console.log(error)
                if (error.name == 'TokenExpiredError') {
                    ctx.status = 401;
                    ctx.body = {
                        message: 'token时间到期'
                    }

                } else {
                    ctx.status = 401;
                    ctx.body = {
                        message: 'token失效'
                    }

                }
            }

        } else {
            ctx.status = 401;
            ctx.body = {
                message: '没有token'
            }
            return;
        }
    }
};